package com.sunmnet.auto.spring.mvc.variable.impl;

import com.sunmnet.auto.spring.mvc.baen.FieldSturcture;
import com.sunmnet.auto.spring.mvc.baen.TBStructure;
import com.sunmnet.auto.spring.mvc.temp.FileType;
import com.sunmnet.auto.spring.mvc.variable.IVarDeal;

public class IndexJspFormCols implements IVarDeal {

	public String excute(FileType type, TBStructure tb) {
		StringBuilder sb = new StringBuilder();
		/*
			<tr>
				<th>账户名称</th>
				<td><input class=\"easyui-textbox\" name=\"Q_username_like\"/></td>
				<th>中文名称</th>
				<td><input class="easyui-textbox" name="Q_nameCn_like"/></td>
				<th>英文名称</th>
				<td><input class="easyui-textbox" name="Q_nameEn_like"/></td>
				<th>手 机</th>
				<td><input class="easyui-textbox" name="Q_cellPhone_like"/></td>
			</tr>
		*/
		int count = 0;
		for (FieldSturcture field : tb.getFields()) {
			if(count % 4 == 0){
				sb.append("\t\t\t\t<tr>\r\n");
			}
			
			count += getTdString(sb, field, count);
			
			count ++;
			if(count % 4 == 0){
				sb.append("\t\t\t\t</tr>\r\n");
			}
		}
		if(count % 4 != 0){
			sb.append("\t\t\t\t</tr>\r\n");
		}
		return sb.toString();
	}
	
	// 如果是DATE 类型的就返回1
	private int getTdString(StringBuilder sb, FieldSturcture field, int count){
		if(field.getType().equalsIgnoreCase("String")){
			sb.append("\t\t\t\t\t<th>").append(field.getPropertyName()).append("</th>\r\n");
			sb.append("\t\t\t\t\t<td><input class=\"easyui-textbox\" name=\"Q_");
			sb.append(field.getPropertyName());
			sb.append("_like\"/></td>\r\n");
			return 0;
		}else if(field.getType().equalsIgnoreCase("Date")){
			/**
					<th>出生日期</th>
					<td><input name="Q_birthday_>_Date" class="easyui-datebox"/>
					</td>
					<th>至</th>
					<td><input name="Q_birthday_<_Date" class="easyui-datebox"/>
					</td>
			 */
			sb.append("\t\t\t\t\t<th>").append(field.getPropertyName()).append("</th>\r\n");
			sb.append("\t\t\t\t\t<td><input class=\"easyui-datebox\" name=\"Q_");
			sb.append(field.getPropertyName());
			sb.append("_>=_Date\"/></td>\r\n");
			
			if(++count % 4 == 0){
				sb.append("\t\t\t\t</tr>\r\n");
				sb.append("\t\t\t\t<tr>\r\n");
			}
			
			sb.append("\t\t\t\t\t<th>").append("至").append("</th>\r\n");
			sb.append("\t\t\t\t\t<td><input class=\"easyui-datebox\" name=\"Q_");
			sb.append(field.getPropertyName());
			sb.append("_<=_Date\"/></td>\r\n");
			
			return 1;
		}else {
			/*
			 * 
			 * <th>性 别</th>
					<td>
						<input id="sex" name="Q_sex_=_Short" hidden="true" value=""/>
						<input id="querySex" name=""/>
					</td>
			 */
			sb.append("\t\t\t\t\t<th>").append(field.getPropertyName()).append("</th>\r\n");
			sb.append("\t\t\t\t\t<td><input class=\"easyui-textbox\" name=\"Q_");
			sb.append(field.getPropertyName());
			sb.append("_=_").append(field.getType()).append("\"/></td>\r\n");
			return 0;
		}
	}
}
